package com.fei.controller;

import com.fei.bean.MusicBean;
import com.fei.bean.SingerBean;
import com.fei.bean.SongListMusicBean;
import com.fei.service.MusicService;
import com.fei.service.SingerService;
import com.fei.service.SongListMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class SongListMusicController {

    @Autowired
    SingerService singerService;
    @Autowired
    MusicService musicService;
    @Autowired
    SongListMusicService songListMusicService;


    //    查看歌曲页面
    @RequestMapping("/admin/toSongListSinger")
    public String toSongListSinger(Model model,Integer song_list_id){
//        查询歌单中的歌曲
        List<SongListMusicBean> list = songListMusicService.selectOneIdSong(song_list_id);
//        查询所有歌手,用于下拉菜单
        List<SingerBean> singerBeanList = singerService.findAllSingerService();

        model.addAttribute("songListMusicBean",list);
        model.addAttribute("singerBeanList",singerBeanList);
        model.addAttribute("song_list_id",song_list_id);
        return "song_list_singer";
    }

    //用于添加歌曲的联动选择
    @RequestMapping("/admin/SelectSong")
    @ResponseBody
    public List<MusicBean> SelectSong(Integer singer_id,Integer song_list_id){
        List<MusicBean> musicBeanList = musicService.selectOneSingerIdService(singer_id);
        List<MusicBean> list = songListMusicService.findAllOneSingIdService(song_list_id, singer_id);
        return list;
    }

    //添加歌单中的歌曲
    @RequestMapping("/admin/insertSongListSinger")
    public String insertSongListSinger(SongListMusicBean bean,Integer[] music_ids){
        List<SongListMusicBean> list = new ArrayList<>();
        SongListMusicBean songListMusicBean = null;
        for(int i = 0;i<music_ids.length;i++){
            songListMusicBean = new SongListMusicBean();
            songListMusicBean.setSinger_id(bean.getSinger_id());
            songListMusicBean.setSong_list_id(bean.getSong_list_id());
            songListMusicBean.setMusic_id(music_ids[i]);
            list.add(songListMusicBean);
        }
        songListMusicService.insertSongListMusicService(list);
        return "redirect:/admin/toSongListSinger?song_list_id="+bean.getSong_list_id();
    }


    @RequestMapping("/admin/delete/deleteOneId")
    public String deleteOneId(int song_list_id, int id){
        songListMusicService.deleteOneIdService(id);
        return "redirect:/admin/toSongListSinger?song_list_id="+song_list_id;
    }

}
